﻿import pygame
from pygame.locals import *
from pgu import gui

from Client import *

class MsgBox(gui.Dialog):
	def __init__(self, msg, caption = ""):
		title = gui.Label(caption)
		MsgLabel = gui.Label(msg)
		gui.Dialog.__init__(self, title, MsgLabel)

class UsersDialog(gui.Dialog):
	def __init__(self, list):
		title = gui.Label("User list")

		my_list = gui.List(width=270, height=260)
		for usr in list.split(":"):
			if usr != "":
				my_list.add(usr)
		my_list.resize()
		my_list.repaint()		

		gui.Dialog.__init__(self, title, my_list)

class GameField():
	def __init__(self):
		self.btns = []
		
	def paintGame(self, fld, makeTurn, gui_c):
		def btnClick(val):
			makeTurn(val['x'], val['y'])
		
		self.clearGame(gui_c)
		l = XOrules.getLfield(fld)
		for y in range(l):
			gui_c.tt.tr()
			for x in range(l):
				btn = gui.Button(fld[y*l + x])
				btn.connect(gui.CLICK, btnClick, {'x':x, 'y':y})
				self.btns.append(btn)
				gui_c.tt.td(btn)
				
	def clearGame(self, gui_c):
		for btn in self.btns:
			gui_c.tt.remove(btn)
		self.btns = []

class GUI(gui.Table):
	def __init__(self, client, **params):
		params['valign'] = -1
		params['padding'] = 5
		params['marging'] = 5
		gui.Table.__init__(self, **params)
		
		self.tr()
		self.td(gui.Label("Name"))
		self.name = gui.Input(value='User1',size=8)
		self.td(self.name,colspan=3)

		self.tr()
		btHi = gui.Button("HI")
		btHi.connect(gui.CLICK, client.hi, None)	
		self.td(btHi)	
		btAI = gui.Button("AI")
		btAI.connect(gui.CLICK, client.createAI, None)
		self.td(btAI)			
				
		self.tr()
		btNew = gui.Button("New game")
		btNew.connect(gui.CLICK, client.newGame, None)
		self.td(btNew)
		btWhoIsHere = gui.Button("who is here?")
		btWhoIsHere.connect(gui.CLICK, client.getUserList, None)
		self.td(btWhoIsHere)
		
		#self.tr()
		#self.td(gui.Label("Who: "))
		#self.who = gui.Group(value='X')
		#self.td(gui.Tool(self.who,gui.Label('X'),value='X'))
		#self.td(gui.Tool(self.who,gui.Label('O'),value='O'))
		self.tr()
		btXParty = gui.Button("X")
		btXParty.connect(gui.CLICK, client.addGamer, "X")	
		self.td(btXParty)	
		btOParty = gui.Button("O")
		btOParty.connect(gui.CLICK, client.addGamer, "O")
		self.td(btOParty)	
		btSParty = gui.Button("S")
		btSParty.connect(gui.CLICK, client.addGamer, "S")
		self.td(btSParty)	
		
		self.tr()
		self.td(gui.Label("Size: "))
		self.sizeSelect = gui.Select(value = 3)
		self.sizeSelect.add("3",3)
		self.sizeSelect.add("4",4)
		self.sizeSelect.add("5",5)
		self.td(self.sizeSelect, colspan = 2)

		self.tr()
		self.td(gui.Label("Min chain: "))
		minChainSelect = gui.Select()
		minChainSelect.add("3",3)
		minChainSelect.add("4",4)
		minChainSelect.add("5",5)
		self.td(minChainSelect, colspan = 2)

		self.tr()
		btNew = gui.Button("Start game")
		btNew.connect(gui.CLICK, client.startGame, None)
		self.td(btNew)
		btExit = gui.Button("Exit")
		btExit.connect(gui.CLICK, client.bye, None)
		self.td(btExit)

		self.tr()
		btSave= gui.Button("Save game")
		btSave.connect(gui.CLICK, client.saveGame, None)
		self.td(btSave)
		btLoad= gui.Button("Load game")
		btLoad.connect(gui.CLICK, client.loadGame, None)
		self.td(btLoad)

		self.tr()
		self.tt = gui.Table(width=100,height=100)
		self.td(self.tt)